#coding=utf-8
__author__ = 'Meteor'
from selenium import webdriver
from selenium.webdriver.chrome.options import Options as chrome
from selenium.webdriver.firefox.options import Options as firefox
from selenium.webdriver.edge.options import Options as edge
import  seting,os
from tools.log import logger

class Driver(object):
    '''
    实例化WebDriver对象
    '''
    def __init__(self):
        brower = seting.BROWER.lower()
        #处理driver路径


        if brower not in ('chrome','firefox','edge','phantomjs'):
            logger.error('%s浏览器不支持。。。。' %brower)
            return
        if brower == "chrome":
            driver_path = os.path.join(seting.BASEPATH, 'driver', 'chromedriver.exe')
            option = chrome()

            option.add_argument('--start-maximized')
            # 判断如果日志级别设置为Debug，则打开界面，否则无界面
            if seting.LOG_LEVER != 10:
                option.add_argument('--headless')


            self.driver = webdriver.Chrome(options=option,executable_path=driver_path)
            logger.info('已经打开%s浏览器' %seting.BROWER)
        elif brower== 'firefox':

            # driver_path = r'D:\python0818\auto_frame\driver\geckodriver.exe'
            driver_path = os.path.join(seting.BASEPATH, 'driver', 'geckodriver.exe')
            option = firefox()
            option.add_argument('--start-maximized')
            # 判断如果日志级别设置为Debug，则打开界面，否则无界面
            if seting.LOG_LEVER != 10:
                option.add_argument('--headless')


            self.driver = webdriver.Firefox(options=option,executable_path=driver_path)
            logger.info('已经打开%s浏览器' % seting.BROWER)
        elif brower == 'edge' :
            driver_path = os.path.join(seting.BASEPATH, 'driver', 'MicrosoftWebDriver.exe')

            self.driver = webdriver.Edge(executable_path=driver_path)
            logger.info('已经打开%s浏览器' % seting.BROWER)
        elif brower == 'phantomjs':
            self.driver = webdriver.PhantomJS()
            logger.info('已经打开%s浏览器' % seting.BROWER)





    def get_driver(self):
        return self.driver


